package cn.juque.common.thread;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.juque.common.base.BaseOperator;

import java.util.Objects;

/**
 * @author nuoka
 * @version 1.0.0
 * <li></li>
 * @date 2022/3/12 21:00
 **/
public class BaseOperatorThreadLocal {

    private static final ThreadLocal<BaseOperator> TREAD_LOCAL = ThreadUtil.createThreadLocal(true);

    public void set(BaseOperator baseOperator) {
        TREAD_LOCAL.set(baseOperator);
    }

    public BaseOperator get() {
        return TREAD_LOCAL.get();
    }

    public void remove() {
        TREAD_LOCAL.remove();
    }

    public String getTenantId() {
        BaseOperator operator = TREAD_LOCAL.get();
        return Objects.isNull(operator) || CharSequenceUtil.isEmpty(operator.getTenantId())
                ? CharSequenceUtil.EMPTY : operator.getTenantId();
    }
}
